package sng.modules.view.components.city.logic
{
	import as3isolib.display.IsoView;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	import as3isolib.graphics.Stroke;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import masputih.pathfinding.astar.AStarGrid;
	
	import sng.modules.view.components.city.logic.event.TileEvent;
	import sng.modules.view.components.city.logic.unit.BaseBuilding;
	import sng.modules.view.components.city.logic.unit.DropAttachment;
	import sng.modules.view.components.city.logic.unit.Tile;
	import sng.modules.view.components.city.state.CityNormalState;
	
	/**
	 * ...
	 * 
	 */
	public class CityView extends IsoView
	{
		
		/**
		 * _groundScene备份
		 * eason 20110712
		 */
		//用于放置attachment附属物，主要是掉落出来的，如金币、货物、经验等
		//重写父类AttachmentScene
		private var _attachmentScene:AttachmentScene;
		
//		private var _groundScene:IsoScene;//用于放置建筑、小人等，做统一的深度排序
		private var _groundScene:GroundScene;
		private var _gridScene:IsoScene;
		private var _grid:IsoGrid;
//		private var _selectedTile:Tile;
		private var _debugMode:Boolean;
		private var panPt:Point;//支持地图定位功能的点
		private var _zoom:Number = 0;//当前的缩放等级，目前游戏开放  两级
		private var removeIndex:uint;
		
		public var astarGrid:AStarGrid;
		public var city:City;
		public var dragStart:Boolean = false;
		private const BACKGROUND_WIDTH:Number=3360;
		private const BACKGROUND_HEIGHT:Number=2100;
		private const OFFSET_Y:Number=-718.9;
		
		public function CityView(_city:City, w:Number = 800, h:Number = 600, gridCols:Number = 20, gridRows:Number = 20, gridCellsize:Number = 20)
		{
			city = _city;
			setSize(w, h);
			createGrid(gridCols, gridRows, gridCellsize);
			createScene(gridCols, gridRows,gridCellsize);
			this.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			//debugMode = true;
			
			setBackground(new BackGround(), 0, 0);
		}
		
		//初始化鼠标指针 eason 20110714
		public function initCityCursor(mouseCursorContainer:Sprite):void
		{
			_groundScene.initCityCursor(mouseCursorContainer);
		}
		
		private function onAddedToStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
			this.addEventListener(MouseEvent.MOUSE_WHEEL, onStageMouseWheel);
			
			grid.addEventListener(MouseEvent.CLICK, onGridMouseClick);
		}
		
		protected function onGridMouseClick(event:Event):void
		{
			if(city.currentState.toString() == "CitySeedState")
			{
				city.setCityState(new CityNormalState(city));
			}
		}
		
		private function onStageMouseDown(e:MouseEvent):void
		{
			panPt = new Point(stage.mouseX, stage.mouseY);
			this.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}
		
		private function onStageMouseMove(e:MouseEvent):void
		{
			dragStart = true;
			var panX:int=(panPt.x - stage.mouseX)/Math.pow(2,_zoom);
			var panY:int=(panPt.y - stage.mouseY)/Math.pow(2,_zoom);
			var p:Point=new Point();
			p.x=currentX+panX;
			p.y=currentY+panY;
//			trace(panX,panY);
			//修正tip的位置 eason 20110717
			for(var i:int=0;i<city.tipContainer.numChildren;i++)
			{
				city.tipContainer.getChildAt(i).x-=(panPt.x - stage.mouseX);
				city.tipContainer.getChildAt(i).y-=(panPt.y - stage.mouseY);
			}
//			city.tipContainer.x+=panX;
//			city.tipContainer.y+=panY;
			var limitedWidth:Number=(BACKGROUND_WIDTH-this.width/currentZoom)/2;
			var limitedTop:Number=OFFSET_Y+this.height/currentZoom/2;
			var limitedBottom:Number=BACKGROUND_HEIGHT+OFFSET_Y-this.height/currentZoom/2;
//			trace("height:"+this.height);
//			trace("limitedBottom:"+limitedBottom);
//			trace("p.y:"+p.y);
			/**
			 * 限制拖动范围
			 * eason 20110716
			 */
			if(p.x<-limitedWidth)
				p.x=-limitedWidth;
			else if(p.x>limitedWidth)
				p.x=limitedWidth;
			if(p.y<limitedTop)
				p.y=limitedTop;
			else if(p.y>limitedBottom)
				p.y=limitedBottom;
			this.panTo(p.x,p.y);
//			trace(panX, panY);
			panPt.x = stage.mouseX;
			panPt.y = stage.mouseY;
//			trace(currentX,currentY);
//			trace(currentX);
		}
		
		private function onStageMouseUp(e:MouseEvent):void
		{
			//判断是否发生了移动 eason 20110717
			if(dragStart==true)
			{
				groundScene.disenableTileClickOnce();
			}
			dragStart = false;
			this.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
			this.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}
		
		private function onStageMouseWheel(e:MouseEvent):void
		{
			zoomFunction(e.delta>0?1:-1);
		}
		
		public function zoomFunction(step:int):void
		{
			if(step > 0 && _zoom<0)
			{
				_zoom +=  1;
			}
			if(step < 0 && _zoom>-1)
			{
				_zoom -=  1;
			}
			this.currentZoom = Math.pow(2,_zoom);
			attachmentScene.setAllDropZoom(1/currentZoom);
			//修正tip的大小
//			setAllTipZoom(currentZoom);
			/**
			 * 检查地图是否超出显示范围
			 * eaosn 20110716
			 */
			checkRange();
		}
		
		/**
		 * 检查地图是否超出显示范围
		 * eaosn 20110716
		 */
		public function checkRange():void
		{
			var p:Point=new Point();
			p.x=currentX;
			p.y=currentY;
			var limitedWidth:Number=(BACKGROUND_WIDTH-this.width/currentZoom)/2;
			var limitedTop:Number=OFFSET_Y+this.height/currentZoom/2;
			var limitedBottom:Number=BACKGROUND_HEIGHT+OFFSET_Y-this.height/currentZoom/2;
			if(p.x<-limitedWidth)
				p.x=-limitedWidth;
			else if(p.x>limitedWidth)
				p.x=limitedWidth;
			if(p.y<limitedTop)
				p.y=limitedTop;
			else if(p.y>limitedBottom)
				p.y=limitedBottom;
			this.panTo(p.x,p.y);
		}
		
		private function handleEnterFrame(e:Event):void
		{
			render(true);
		}
		
		//直接调用groundScene中的方法 eason 20110719
//		public function addTile(tile:Tile, col:Number, row:Number, zeroHeight:Boolean=false):void
//		{
//			_groundScene.addTile(tile,col,row,this,zeroHeight);	
//		}
		
		//直接调用attachmentScene中的方法 eason 20110719
//		public function addDrop(tile:Tile, col:Number, row:Number):void
//		{
//			_attachmentScene.addDrop(tile,col,row);
//		}
		
		//直接调用groundScene中的方法 eason 20110719
//		public function removeTile(tile:Tile):void
//		{
//			_groundScene.removeTile(tile);
//		}
		
		//直接调用attachmentScene中的方法 eason 20110719
//		public function removeDrop(tile:Tile):void
//		{
//			_attachmentScene.removeDrop(tile);
//		}
		//直接调用attachmentScene中的方法 eason 20110719
//		public function setAllDropZoom(zoom:int):void
//		{
//			_attachmentScene.setAllDropZoom(zoom);
//		}
		
		//直接调用groundScene中的方法 eason 20110719
//		public function removeAllTile():void
//		{
//			_groundScene.removeAllTile();
//		}
		
		public function setBackground(bg:DisplayObject, x:Number = 0, y:Number = 0):void
		{
			bg.x = x;
			bg.y = y;
			backgroundContainer.addChild(bg);
		}
		
		private function createScene(gridCols:int, gridRows:int,gridCellsize:int):void
		{
			_groundScene=new GroundScene(gridCols,gridRows,gridCellsize,city,this);
			addScene(_groundScene);
			
			_attachmentScene = new AttachmentScene;
			addScene(_attachmentScene);
		}
		
		private function createGrid(cols:Number, rows:Number, cellsize:Number = 20):void
		{
			_gridScene = new IsoScene();
//			addScene(_gridScene);

			_grid = new IsoGrid();
			_grid.showOrigin = false;
			_grid.cellSize = cellsize;
			_grid.setGridSize(50, 50);
			_gridScene.addChild(_grid);
			
			astarGrid = new AStarGrid(cols, rows);
			astarGrid.setAllWalkable();
			
		}
		
		public function get grid():IsoGrid
		{
			return _grid;
		}
		
		public function get debugMode():Boolean
		{
			return _debugMode;
		}
		
		public function set debugMode(value:Boolean):void
		{
			_debugMode = value;
			if (_debugMode)
			{
				if (!containsScene(_gridScene))
				{
					addSceneAt(_gridScene, 0);
				}
			}
			else
			{
				if (containsScene(_gridScene))
				{
					removeScene(_gridScene);
				}
			}
		}
		
		public function get groundScene():GroundScene
		{
			return _groundScene;
		}

//		public function set groundScene(value:GroundScene):void
//		{
//			_groundScene = value;
//		}
		
		public function get attachmentScene():AttachmentScene
		{
			return _attachmentScene;
		}

		public function getChildByID(id:String):BaseBuilding
		{
			var child:BaseBuilding = groundScene.getChildByID(id) as BaseBuilding;
			
			return child;
		}
	}
}